$(function () {
    // 选择日期
    var format = 'YYYY-MM-DD';
    $('#datePickerInput').daterangepicker({
        'showDropdowns': true,
        'autoApply': true,
        'dateLimit': {
            'days': 30
        },
        'locale': {
            'format': format,
            'applyLabel': lang('date.ensure'),
            'cancelLabel': lang('date.cancel'),
            'daysOfWeek': [
                lang('week.7'),
                lang('week.1'),
                lang('week.2'),
                lang('week.3'),
                lang('week.4'),
                lang('week.5'),
                lang('week.6')
            ],
            'monthNames': [
                lang('month.1'),
                lang('month.2'),
                lang('month.3'),
                lang('month.4'),
                lang('month.5'),
                lang('month.6'),
                lang('month.7'),
                lang('month.8'),
                lang('month.9'),
                lang('month.10'),
                lang('month.11'),
                lang('month.12')
            ],
            'firstDay': 1
        },
        'linkedCalendars': false,
        'parentEl': 'body',
        'startDate': $('#datePicker').find('.start').html(),
        'endDate': $('#datePicker').find('.end').html(),
        'minDate': '2000-01-01',
        'maxDate': '2038-01-01',
        'opens': 'left'
    }, function (start, end, label) {
        $('#datePicker').find('.start').html(start.format(format));
        $('#datePicker').find('.end').html(end.format(format));
        refreshPage();
    });
    $('#datePickerContainer').on('click', function () {
        $('#datePickerInput').trigger('focus');
    });
    var refreshPage = function () {
        var startDate = $('#datePicker').find('.start').html();
        var endDate = $('#datePicker').find('.end').html();
        var param = {};
        param.hotel = window.currentHotel;
        param.start = startDate;
        param.end = endDate;
        showContentLoading();
        ajaxRequest('/Home/Report/getOverview', param, 'post', function (data) {
            var info = data.data;
            var yesterdayData = info.yesterday;
            var todayData = info.today;
            // 财务收支
            var financeOverview = $('#statisticOverview');
            financeOverview.find('span[data-tag="financesurplus"]').html(info.financesurplus);
            financeOverview.find('span[data-tag="surplus"]').html(info.surplus);
            financeOverview.find('span[data-tag="income"]').html(info.income);
            financeOverview.find('span[data-tag="outcome"]').html(info.outcome);
            financeOverview.find('span[data-tag="notesurplus"]').html(info.notesurplus);
            financeOverview.find('span[data-tag="noteincome"]').html(info.noteincome);
            financeOverview.find('span[data-tag="noteoutcome"]').html(info.noteoutcome);
            // 运营概览
            // 间夜房
            var yesterdayOverview = $('#yesterdayOverview');
            yesterdayOverview.find('td[data-tag="income"]').html(yesterdayData.income);
            yesterdayOverview.find('td[data-tag="roomCount"]').html(yesterdayData.roomCount);
            yesterdayOverview.find('td[data-tag="occupancyRate"]').html(yesterdayData.occupancyRate);
            yesterdayOverview.find('td[data-tag="price"]').html(yesterdayData.price);
            yesterdayOverview.find('td[data-tag="notSold"]').html(yesterdayData.notSold);
            yesterdayOverview.find('td[data-tag="notCheckIn"]').html(yesterdayData.notCheckIn);
            var todayOverview = $('#todayOverview');
            todayOverview.find('td[data-tag="income"]').html(todayData.income);
            todayOverview.find('td[data-tag="roomCount"]').html(todayData.roomCount);
            todayOverview.find('td[data-tag="occupancyRate"]').html(todayData.occupancyRate);
            todayOverview.find('td[data-tag="price"]').html(todayData.price);
            todayOverview.find('td[data-tag="notSold"]').html(todayData.notSold);
            todayOverview.find('td[data-tag="notCheckIn"]').html(todayData.notCheckIn);
            // 钟点房
            if (info.yesterdayHour !== undefined) {
                var yesterdayHourData = info.yesterdayHour;
                var yesterdayHourOverview = $('#yesterdayHourOverview');
                yesterdayHourOverview.find('td[data-tag="income"]').html(yesterdayHourData.income);
                yesterdayHourOverview.find('td[data-tag="roomCount"]').html(yesterdayHourData.roomCount);
                yesterdayHourOverview.find('td[data-tag="occupancyRate"]').html(yesterdayHourData.occupancyRate);
                yesterdayHourOverview.find('td[data-tag="price"]').html(yesterdayHourData.price);
                yesterdayHourOverview.find('td[data-tag="notSold"]').html(yesterdayHourData.notSold);
                yesterdayHourOverview.find('td[data-tag="notCheckIn"]').html(yesterdayHourData.notCheckIn);
                var todayHourData = info.todayHour;
                var todayHourOverview = $('#todayHourOverview');
                todayHourOverview.find('td[data-tag="income"]').html(todayHourData.income);
                todayHourOverview.find('td[data-tag="roomCount"]').html(todayHourData.roomCount);
                todayHourOverview.find('td[data-tag="occupancyRate"]').html(todayHourData.occupancyRate);
                todayHourOverview.find('td[data-tag="price"]').html(todayHourData.price);
                todayHourOverview.find('td[data-tag="notSold"]').html(todayHourData.notSold);
                todayHourOverview.find('td[data-tag="notCheckIn"]').html(todayHourData.notCheckIn);
                $('#hour-room').removeClass('hidden');
            }
            // 长租房
            if (info.yesterdayMonth !== undefined) {
                var yesterdayLongData = info.yesterdayMonth;
                var yesterdayLongOverview = $('#yesterdayLongOverview');
                yesterdayLongOverview.find('td[data-tag="income"]').html(yesterdayLongData.income);
                yesterdayLongOverview.find('td[data-tag="roomCount"]').html(yesterdayLongData.roomCount);
                yesterdayLongOverview.find('td[data-tag="occupancyRate"]').html(yesterdayLongData.occupancyRate);
                yesterdayLongOverview.find('td[data-tag="price"]').html(yesterdayLongData.price);
                yesterdayLongOverview.find('td[data-tag="notSold"]').html(yesterdayLongData.notSold);
                yesterdayLongOverview.find('td[data-tag="notCheckIn"]').html(yesterdayLongData.notCheckIn);
                var todayLongData = info.todayMonth;
                var todayLongOverview = $('#todayLongOverview');
                todayLongOverview.find('td[data-tag="income"]').html(todayLongData.income);
                todayLongOverview.find('td[data-tag="roomCount"]').html(todayLongData.roomCount);
                todayLongOverview.find('td[data-tag="occupancyRate"]').html(todayLongData.occupancyRate);
                todayLongOverview.find('td[data-tag="price"]').html(todayLongData.price);
                todayLongOverview.find('td[data-tag="notSold"]').html(todayLongData.notSold);
                todayLongOverview.find('td[data-tag="notCheckIn"]').html(todayLongData.notCheckIn);
                $('#long-room').removeClass('hidden');
            }
            $('#roomOverview>.content-panel-header').find('a').attr('href', '/Home/Report/exportList?hotel=' + window.currentHotel + '&start=' + $('#datePicker').find('.start').html() + '&end=' + $('#datePicker').find('.end').html());
            hideContentLoading();
        }, function (error) {
            hideContentLoading();
            if (error.responseJSON !== undefined) {
                modalAlert(error.responseJSON.info);
            }
        });
    };
    var showContentLoading = function () {
        $('.content-panel .content-panel-loading').removeClass('hidden');
    };
    var hideContentLoading = function () {
        $('.content-panel .content-panel-loading').addClass('hidden');
    };
    refreshPage();
});
